<template>
    <div>
        <el-alert
            title="未设置的渠道将使用默认页面."
            type="info"
            :closable="false"
            show-icon>
        </el-alert>
        <el-divider></el-divider>
        <div class="filter-container">
            <el-form label-position="left" :inline="true" class="mt-10">
                <el-form-item label="应用渠道">
                    <select-app-source v-model="search.sources"></select-app-source>
                </el-form-item>
                <el-form-item label="页面类型">
                    <el-select v-model="search.type" clearable class="filter-item" placeholder="页面类型" style="width: 160px;">
                        <el-option
                            v-for="(item, index) in options.types"
                            :key="index"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="页面名称">
                    <el-input class="filter-item" clearable v-model="search.name" placeholder="页面名称"/>
                </el-form-item>
                <el-form-item label="是否默认">
                    <el-select v-model="search.is_default" clearable class="filter-item" placeholder="默认" style="width: 100px;">
                        <el-option label="是" :value="1" ></el-option>
                        <el-option label="否" :value="0" ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="">
                    <el-button class="filter-item" icon="el-icon-search" type="primary" :loading="loading" @click="pageNo = 1;handleSearch()">搜索</el-button>
                    <el-button class="filter-item" icon="el-icon-plus" type="primary" @click="create" plain>添加</el-button>
                </el-form-item>
            </el-form>
        </div>

        <vxe-table
            v-loading="loading"
            ref="xTable"
            :data="tableData"
            style="width: 100%"
            @checkbox-all="selectChangeEvent"
            @checkbox-change="selectChangeEvent">
            <vxe-table-column type="checkbox" width="40" fixed="left"/>
            <vxe-table-column title="页面类型" field="type" width="100">
                <template #default="{row}">
                    <el-tag>{{ options.types[row.type].label }}</el-tag>
                </template>
            </vxe-table-column>
            <vxe-table-column title="是否默认" field="is_default" width="80" formatter="formatBoolText"/>
            <vxe-table-column title="页面名称" field="name" width="180"/>
            <vxe-table-column title="页面地址" field="full_path" min-width="250">
                <template #default="{row}">
                    <el-link :href="row.full_path" target="_blank">{{ row.full_path }}</el-link>
                </template>
            </vxe-table-column>
            <vxe-table-column title="应用渠道" field="sources" min-width="400"/>
            <vxe-table-column title="操 作" width="150px">
                <template #default="{row}">
                    <el-button
                        type="primary"
                        @click="edit(row)">
                        编辑
                    </el-button>
                    <destroy-confirm
                        route="appWebpage/Destroy"
                        :primary="row.id"
                        @success="handleSearch">
                    </destroy-confirm>
                </template>
            </vxe-table-column>
        </vxe-table>

        <div class="mt-10 pull-left">
            <destroy-multiple
                route="appWebpage/DestroyMultiple"
                :selected="selected"
                @success="handleSearch">
                <el-button type="danger"  plain>删除选中</el-button>
            </destroy-multiple>
        </div>

        <el-pagination
            class="pull-right"
            background
            :current-page.sync="pageNo"
            :page-sizes="pageOptions.pageSizesList"
            :page-size="pageSize"
            :layout="pageOptions.layout"
            :total="totalDataNumber"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange">
        </el-pagination>

        <el-dialog
            :title="form.data && form.data.id ? '编辑' : '添加'"
            :visible.sync="form.show"
            width="90%"
            :append-to-body="true"
            :close-on-click-modal="false"
            center>
            <dialog-form
                v-if="form.show"
                :data="form.data"
                v-on:submit="onFormSubmit"
                v-on:cancel="form.show = false">
            </dialog-form>
        </el-dialog>

    </div>
</template>

<script>
import dialogForm from './form'

export default {
    name: 'index',
    components: {
        dialogForm,
    },
    data() {
        return {
            search: {
                type: '',
                sources: '',
                name: '',
                is_default: '',
            },
            loading: false,
            selected: [],
            tableData: [],
            pageNo: 1,
            pageSize: 20,
            pageOptions: this.$store.state.backend.pageOptions,
            totalDataNumber: 0,
            options: this.$store.state.appWebpage.options,
            form: {
                data: {},
                show: false,
            }
        }
    },
    methods: {
        selectChangeEvent ({ checked, records }) {
            this.selected = records.map(v => v.id);
            // console.log(checked ? '勾选事件' : '取消事件', records)
        },
        handleSizeChange(val) {
            this.pageNo = 1;
            this.pageSize = parseInt(`${val}`);
            this.handleSearch();
        },
        handleCurrentChange(val) {
            this.pageNo = parseInt(`${val}`);
            this.handleSearch();
        },
        handleSearch() {
            this.loading = true;
            let filter = Object.assign({}, this.search, {
                page: this.pageNo,
                pageSize: this.pageSize,
            });
            this.$store.dispatch('appWebpage/Lists', filter).then(res => {
                this.tableData = res.data.data;
                this.totalDataNumber = res.data.meta.total;
            }).finally(() => {
                this.loading = false;
            });
        },
        initData() {
            this.handleSearch();
        },
        create() {
            this.form.data = {};
            this.form.show = true;
        },
        edit(row) {
            this.form.data = row;
            this.form.show = true;
        },
        onFormSubmit() {
            this.form.show = false;
            this.handleSearch();
        },
        onDestroy() {

        }
    },
    created() {
        this.initData();
    }
}
</script>

<style lang="scss" scoped>
#app .el-form-item {
    margin-bottom: 0;
}
</style>
